package com.module.lemlin.custom;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ImageSpan;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.ConvertUtils;

import java.util.List;

public class TagTextView extends AppCompatTextView {
    public TagTextView(@NonNull Context context) {
        super(context);
    }

    public TagTextView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public TagTextView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setTextTag(String text, List<String> tags) {
        StringBuffer textTagBuffer = new StringBuffer();
        for (String item : tags) {
            textTagBuffer.append(item);
        }
        textTagBuffer.append(text);
        SpannableString spannableString = new SpannableString(textTagBuffer);
        for (int i = 0; i < tags.size(); i++) {
            String item = tags.get(i);
            GradientDrawable gradientDrawable1 = new GradientDrawable();
            gradientDrawable1.setShape(GradientDrawable.RECTANGLE);
            gradientDrawable1.setCornerRadius(ConvertUtils.dp2px(20f));
            gradientDrawable1.setColor(Color.parseColor("#FF2B5D"));
            FrameLayout frameLayout = new FrameLayout(getContext());
            TextView tagTextView = new TextView(getContext());
            FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, ConvertUtils.dp2px(18f));
            layoutParams.rightMargin = ConvertUtils.dp2px(5f);
            layoutParams.bottomMargin = ConvertUtils.dp2px(3f);
            tagTextView.setPadding(ConvertUtils.dp2px(10f), ConvertUtils.dp2px(1f), ConvertUtils.dp2px(10f), ConvertUtils.dp2px(1f));
            tagTextView.setTextColor(Color.WHITE);
            tagTextView.setBackground(gradientDrawable1);
            tagTextView.setTextSize(11);
            tagTextView.setText(item);
            tagTextView.setLayoutParams(layoutParams);
            frameLayout.removeAllViews();
            frameLayout.addView(tagTextView);
            Bitmap bitmap = convertViewToBitmap(frameLayout);
            Drawable drawable = new BitmapDrawable(bitmap);
            drawable.setBounds(0, 0, tagTextView.getWidth(), tagTextView.getHeight());
            ImageSpan imageSpan = new ImageSpan(drawable, ImageSpan.ALIGN_BOTTOM);
            int startIndex = getLastLength(tags, i);
            int endIndex = startIndex + item.length();
            spannableString.setSpan(imageSpan, startIndex, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        setText(spannableString);
        setGravity(Gravity.CENTER_VERTICAL);
    }

    private static Bitmap convertViewToBitmap(View view) {
        view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
        view.buildDrawingCache();
        return view.getDrawingCache();
    }

    private int getLastLength(List<String> list, int maxLength) {
        int length = 0;
        for (int i = 0; i < maxLength; i++) {
            length += list.get(i).length();
        }
        return length;
    }

}
